﻿// original Source by internet.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;

// ReSharper disable CheckNamespace
namespace DevPhone.Controls
// ReSharper restore CheckNamespace
{
    public static class LocalBrowser
    {
        public const string TokenMessageQueryString = "Parameters";

        public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
            "Html", typeof(string), typeof(WebBrowser), new PropertyMetadata(OnHtmlChanged));

        /// <summary>
        /// Create a string with QueryString Page
        /// </summary>
        /// <param name="page">The parameter of Page</param>
        /// <returns>Return the query string</returns>
        public static string MountQueryString(string page)
        {
            //Variables
            string ret = string.Empty;

            //Verify the querystring
            if (!string.IsNullOrEmpty(page))
            {
                //Add the query string
                ret += ("?PAGE=" + page);
            }

            return ret;
        }

        /// <summary>
        /// Create a string with QueryString
        /// </summary>
        /// <param name="queryStrings">The QueryStrings</param>
        /// <returns>Return a new string with QueryStrings</returns>
        public static string MountQueryString(Dictionary<string, string> queryStrings)
        {
            //Variables
            var ret = string.Empty;
            // Verify the QueryString
            if (queryStrings != null && queryStrings.Count > 0)
            {
                //Add the ? caracter to start the querystring
                ret += "?";
                // Get each QueryString on the list to add on xaml string
                ret = queryStrings.Aggregate(ret,
                                             (current, queryString) =>
                                             current + (queryString.Key + "=" + queryString.Value));

                ret = ret.ToUpperInvariant();
            }
            // return the object
            return ret;
        }

        //Register a dependency property to use on web browser


        public static string GetHtml(DependencyObject dependencyObject)
        {
            //Get the value
            if (dependencyObject == null) return null;
            return (string) dependencyObject.GetValue(HtmlProperty);
        }


        public static void SetHtml(DependencyObject dependencyObject, string value)
        {
            //Set the value
            if (dependencyObject != null)
            {
                dependencyObject.SetValue(HtmlProperty, value);
            }
        }


        private static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //Get the WebBrowser
            var browser = d as WebBrowser;
            //Verify if its null
            if (browser == null)
                return;
            //Get the html page
            var html = e.NewValue.ToString();
            //Change the page of the webBrowser
            browser.NavigateToString(html);
        }
    }
}